#include <nds.h>
#include <stdio.h>
#include <dswifi9.h>
#include <fat.h>
//#include <filesystem.h>

#include "debug.h"
#include "browser.h"
#include "player.h"
#include "scrobbler.h"
#include "graphics.h"
#include "command.h"
#include "decoder_meta.h"

Browser browser;
DecoderMeta decoders;
Graphics graphics;
Player player;
Scrobbler scrobbler;

#ifdef DEBUG_TIMES
u32 dbgTimerInt = 0;
void dbgTimer()
{
	++dbgTimerInt;
}
#endif

#ifdef DEBUG_FILEOUT
	FILE *DBGFILE;
#endif

enum UIButton {
	BUTTON_NONE,
	BUTTON_OUT,
	BUTTON_PLAYPAUSE,
	BUTTON_STOP,
	BUTTON_PREV,
	BUTTON_NEXT,
	BUTTON_SWITCH
};

// ---------------------------------------------------------------------------
UIButton stylusInArea()
// ---------------------------------------------------------------------------
{
	touchPosition touchData;
	touchRead(&touchData);

	if (!(keysHeld() & KEY_TOUCH)) return BUTTON_NONE;

	if ((touchData.px-18.5)*(touchData.px-18.5) + (touchData.py-15.5)*(touchData.py-15.5) <= 256)
	{
		return BUTTON_PLAYPAUSE;
	}
	else
	if ((touchData.px-41.5)*(touchData.px-41.5) + (touchData.py-21.5)*(touchData.py-21.5) <= 64)
	{
		return BUTTON_STOP;
	}
	else
	if ((touchData.px-59.5)*(touchData.px-59.5) + (touchData.py-21.5)*(touchData.py-21.5) <= 64)
	{
		return BUTTON_PREV;
	}
	else
	if ((touchData.px-77.5)*(touchData.px-77.5) + (touchData.py-21.5)*(touchData.py-21.5) <= 64)
	{
		return BUTTON_NEXT;
	}
	else
	if (221 <= touchData.px && touchData.px <= 250 && 15 <= touchData.py && touchData.py <= 27)
	{
		return BUTTON_SWITCH;
	}
	else return BUTTON_OUT;
}

//---------------------------------------------------------------------------------
int main()
//---------------------------------------------------------------------------------
{
	irqEnable(IRQ_VBLANK | IRQ_TIMER0 | IRQ_TIMER1 | IRQ_TIMER2);
	#ifdef DEBUG_TIMES
		TIMER3_DATA = TIMER_FREQ(1000);
		TIMER3_CR = TIMER_DIV_1 | TIMER_ENABLE | TIMER_IRQ_REQ;
		irqSet(IRQ_TIMER3, dbgTimer);
		irqEnable(IRQ_TIMER3);
	#endif

	#ifndef DEBUG
		graphics.init();
	#else
		consoleDemoInit();
	#endif

	fatInitDefault();
	browser.setPath("fat:/");
	scrobbler.readCfg();

	/*
	if (!nitroFSInit())
	{
		#ifdef DEBUG
		iprintf("NitroFS init failed!\n");
		#endif
	}
	*/
	player.playlist.add("fat:/test.flac");

	int keys, held;
	bool lcdon = true;
	bool screenPressed = false;
	UIButton activeButton = BUTTON_NONE;
	UIButton hoveredButton = BUTTON_NONE;
	bool now_playlist = false;

	while (1) {
		if (!player.process())
		{
			swiWaitForVBlank();
		}
		scanKeys();
		keys = keysDown();
		held = keysHeld();
		
		if (held & KEY_LID)
		{
			if (lcdon)
			{
				powerOff(POWER_LCD);
				lcdon = false;
			}
		}
		else
		{
			if (!lcdon)
			{
				powerOn(POWER_LCD);
				lcdon = true;
			}
			switch (keys)
			{
				case KEY_L:
					player.prev();
				break;
				case KEY_R:
					player.next();
				break;
				case KEY_B:
					player.play();
				break;
				case KEY_A:
					player.stop();
				break;

				case KEY_TOUCH:
					screenPressed = true;
					activeButton = stylusInArea();
				break;
			}
			#ifndef DEBUG
			if (held & KEY_TOUCH)
			{
				hoveredButton = stylusInArea();
				if (hoveredButton == activeButton)
				{
					switch (hoveredButton)
					{
						case BUTTON_PLAYPAUSE:					
							if (player.getStatus() == PLAYER_PLAY)
							{
								graphics.setButtonHighlight(Graphics::BUTTON_PAUSE, true);
							}
							else
							{					
								graphics.setButtonHighlight(Graphics::BUTTON_PLAY, true);
							}
						break;
						case BUTTON_STOP:
							graphics.setButtonHighlight(Graphics::BUTTON_STOP, true);
						break;
						case BUTTON_PREV:
							graphics.setButtonHighlight(Graphics::BUTTON_PREV, true);
						break;
						case BUTTON_NEXT:
							graphics.setButtonHighlight(Graphics::BUTTON_NEXT, true);
						break;
						case BUTTON_SWITCH:
							graphics.setButtonHighlight(Graphics::BUTTON_SWITCH, true);
						break;
						default: break;
					}
				}
				else
				{
					if (player.getStatus() == PLAYER_PLAY)
					{
						graphics.setButtonHighlight(Graphics::BUTTON_PAUSE, false);
					}
					else
					{					
						graphics.setButtonHighlight(Graphics::BUTTON_PLAY, false);
					}
				}
			}
			else if (screenPressed)
			{
				screenPressed = false;
				if (hoveredButton == activeButton)
				{
					switch (activeButton)
					{
						case BUTTON_PLAYPAUSE:
							player.toggleplay();
						break;
						case BUTTON_STOP:
							player.stop();
						break;
						case BUTTON_PREV:
							player.prev();
						break;
						case BUTTON_NEXT:
							player.next();
						break;
						case BUTTON_SWITCH:
							player.togglepause();
							(now_playlist) ? browser.redraw() : player.playlist.redraw();
							player.togglepause();
							now_playlist = !now_playlist;
						break;
						default: break;
					}
				}
				activeButton = hoveredButton = BUTTON_NONE;
				if (player.getStatus() == PLAYER_PLAY)
				{
					graphics.setButtonHighlight(Graphics::BUTTON_PAUSE, false);
				}
				else
				{
					graphics.setButtonHighlight(Graphics::BUTTON_PLAY, false);
				}
			}
			#endif
		}
	}

	return 0;
}
